﻿// 标准管理平台

using Microsoft.Extensions.Logging;
using System.Text.Json;

namespace Passion.iWMS.Application.Service;
/// <summary>
/// 中达集成 🧩
/// </summary>
[ApiDescriptionSettings(ApplicationConst.ApiGateway, Order = 1)]
public class ApiGateway : IDynamicApiController, IScoped
{
    private readonly ILogger _logger;
    private readonly IServiceScopeFactory _scopeFactory;
    private readonly SqlSugarRepository<ASRS_StockOutCommand_3_1> _asrs_stockoutcommand;
    private readonly SqlSugarRepository<ASRS_StockOutCommand_Details_3_1> _asrs_stockoutcommand_details;

    public ApiGateway(ILoggerFactory logger,
                       IServiceScopeFactory scopeFactory,
                       SqlSugarRepository<ASRS_StockOutCommand_3_1> asrs_stockoutcommand,
                       SqlSugarRepository<ASRS_StockOutCommand_Details_3_1> asrs_stockoutcommand_details)
    {
        _logger = logger.CreateLogger(CommonConst.SysLogCategoryName);
        _scopeFactory = scopeFactory;
        _asrs_stockoutcommand = asrs_stockoutcommand;
        _asrs_stockoutcommand_details = asrs_stockoutcommand_details;
    }

    /// <summary>
    /// 1.1庫存狀態上報
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [DisplayName("1.1庫存狀態上報 ASRS->Host")]
    [AllowAnonymous]
    [NonUnify]
    [HttpPost("/api/WCS/BinSyncReport")]
    public async Task<bool> BinSyncReport()
    {
        return true;
    }

    /// <summary>
    /// 1.2 庫存狀態請求
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [DisplayName("1.2 庫存狀態請求 Host->ASRS")]
    [AllowAnonymous]
    [NonUnify]
    [HttpPost("/api/ASRS/BinSyncRequest")]
    public async Task<bool> BinSyncRequest()
    {
        return true;
    }

    /// <summary>
    /// 2.1入庫確認
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [DisplayName("2.1入庫確認 ASRS->Host")]
    [AllowAnonymous]
    [NonUnify]
    [HttpPost("/api/WCS/StockInRequest")]
    public async Task<bool> StockInRequest()
    {
        return true;
    }

    /// <summary>
    /// 2.2入庫完成
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [DisplayName("2.2入庫完成 ASRS->Host")]
    [AllowAnonymous]
    [NonUnify]
    [HttpPost("/api/WCS/StockInCompleted")]
    public async Task<bool> StockInCompleted()
    {
        return true;
    }


    /// <summary>
    /// 3.1發料請求
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [DisplayName("3.1發料請求 Host->ASRS")]
    [AllowAnonymous]
    [NonUnify]
    [HttpPost("/api/ASRS/StockOutCommand")]
    public async Task<bool> StockOutCommand(ASRS_StockOutCommand_3_1_Input dto)
    {
        if (dto.BinList == null)
        {
            throw Oops.Oh("Bin数据不能为空。");
        }
        var options = new JsonSerializerOptions
        {
            PropertyNamingPolicy = null
        };

        var json = JsonSerializer.Serialize(dto, options);

        dto.ReportRemarks = json;
        foreach (var binItem in dto.BinList)
        {
            await _asrs_stockoutcommand_details.InsertAsync(binItem.Adapt<ASRS_StockOutCommand_Details_3_1>());
        }
        dto.BinList = null;
        bool result_Main = await _asrs_stockoutcommand.InsertAsync(dto.Adapt<ASRS_StockOutCommand_3_1>());

        if (!result_Main)
        {
            return false;
        }
        return true;
    }

    /// <summary>
    /// 3.2出庫完成
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [DisplayName("3.2出庫完成 ASRS->Host")]
    [AllowAnonymous]
    [NonUnify]
    [HttpPost("/api/WCS/StockOutCompleted")]
    public async Task<bool> StockOutCompleted()
    {
        return true;
    }

    /// <summary>
    /// 3.3人工出庫
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [DisplayName("3.3人工出庫 ASRS->Host")]
    [AllowAnonymous]
    [NonUnify]
    [HttpPost("/api/WCS/ManualStockOutReport")]
    public async Task<bool> ManualStockOutReport()
    {
        return true;
    }

    /// <summary>
    /// 4.1庫存資訊更新 
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [DisplayName("4.1庫存資訊更新 Host->ASRS")]
    [AllowAnonymous]
    [NonUnify]
    [HttpPost("/api/ASRS/RegisterInfoUpdate")]
    public async Task<bool> RegisterInfoUpdate()
    {
        return true;
    }

    /// <summary>
    /// 5.1 系統存活確認 
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [DisplayName("5.1 系統存活確認 Host->ASRS")]
    [AllowAnonymous]
    [NonUnify]
    [HttpPost("/api/ASRS/AliveCheck")]
    public async Task<bool> AliveCheck()
    {
        return true;
    }

    /// <summary>
    /// 5.2 校時需求 
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [DisplayName("5.2 校時需求 ASRS->Host")]
    [AllowAnonymous]
    [NonUnify]
    [HttpPost("/api/WCS/DateTimeSynchronizeRequest")]
    public async Task<bool> DateTimeSynchronizeRequest()
    {
        return true;
    }

    /// <summary>
    /// 5.3 設備狀態上傳 
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [DisplayName("5.3 設備狀態上傳 ASRS->Host")]
    [AllowAnonymous]
    [NonUnify]
    [HttpPost("/api/WCS/EQStatusReport")]
    public async Task<bool> EQStatusReport()
    {
        return true;
    }

    /// <summary>
    /// 5.4 設備狀態請求 
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [DisplayName("5.4 設備狀態請求 Host->ASRS")]
    [AllowAnonymous]
    [NonUnify]
    [HttpPost("/api/ASRS/EQStatusRequest")]
    public async Task<bool> EQStatusRequest()
    {
        return true;
    }

    /// <summary>
    /// 5.5 異常警報事件通知 
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [DisplayName("5.5 異常警報事件通知  ASRS->Host")]
    [AllowAnonymous]
    [NonUnify]
    [HttpPost("/api/WCS/AlarmReport")]
    public async Task<bool> AlarmReport()
    {
        return true;
    }


    /// <summary>
    /// 5.6 異常警報查詢
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [DisplayName("5.6 異常警報查詢 Host->ASRS")]
    [AllowAnonymous]
    [NonUnify]
    [HttpPost("/api/ASRS/GetAlarms")]
    public async Task<bool> GetAlarms()
    {
        return true;
    }


    /// <summary>
    /// 5.7 貼標資訊請求
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [DisplayName("5.7 貼標資訊請求 ASRS->Host")]
    [AllowAnonymous]
    [NonUnify]
    [HttpPost("/api/WCS/LabelingRequest")]
    public async Task<bool> LabelingRequest()
    {
        return true;
    }

}
